package cn.org.xiaosheng.core.factory;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author XiaoSheng
 * @date 2024/8/20 下午10:10
 */
@Slf4j
public class ServiceFactory {

    //保存所有有注解@RpcService的集合
    public static final Map<String, Object> serviceFactory = new ConcurrentHashMap<>();

    // 添加已注解的类进入工厂
    public <T> void addServiceProvider(String serviceName, T service) {
        if (serviceFactory.containsKey(serviceName)) {
            return;
        }
        serviceFactory.put(serviceName, service);
        log.debug("服务类{}添加进工厂",serviceName);
    }

    // 获取远程调用服务
    public Object getServiceProvider(String serviceName) {
        Object service = serviceFactory.get(serviceName);
        if (service == null) {
            throw new RuntimeException("未发现该服务");
        }
        return service;
    }

}
